package com.forezp.configclient.rabbitConfig;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.listener.RabbitListenerErrorHandler;
import org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @version V1.0  </br>
 * @ClassName RabbitMqListener </br>
 * @Description 一个服务中可以有多个@RabbitListener注解的方法消费一个queue，
 *  一条消息也不会被两个方法都调用，如果RabbitListenerContainerFactory中设置concurrentConsumer为3，
 *  意味着每个方法产生3个consumer  </br>
 * @Author tonywang</br>
 * @DATE 11/22/2018 1:54 PM</br>
 */
@Component
public class RabbitMqListener {

    /*两个error handler类可以对@RabbitListener注解的方法中抛出的异常进行处理。
    一个是RabbitListenerErrorHandler接口，并将其设置到@RabbitListener注解中
    另一个是ErrorHandler接口，并将其设置到RabbitListenerContainerFactory中。
    */
    @Bean
    public RabbitListenerErrorHandler rabbitListenerErrorHandler(){
        return new RabbitListenerErrorHandler() {
            @Override
            public Object handleError(Message amqpMessage, org.springframework.messaging.Message<?> message, ListenerExecutionFailedException exception) throws Exception {
                System.out.println(message);
                throw exception;
            }
        };
    }

    //direct和topic类型的exchange需要routingKey，示例：
//    @RabbitListener(errorHandler = "rabbitListenerErrorHandler",bindings = @QueueBinding(
//            value = @Queue(value = "myQueue", durable = "true"),
//            exchange = @Exchange(value = "auto.exch", durable = "true"),
//            key = "orderRoutingKey.#")
//    )
    public void listen(Message message) {
    }

    //fanout类型的exchange
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "myQueue", durable = "true"),
//            exchange = @Exchange(value = "auto.exch", durable = "true", type = ExchangeTypes.FANOUT)
//        )
//    )
    public void listen1(Message message) {
    }

    //并且支持arguments属性，可用于headers类型的exchange，示例：
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "auto.headers", autoDelete = "true",
//                    arguments = @Argument(name = "x-message-ttl", value = "10000",
//                            type = "java.lang.Integer")),
//            exchange = @Exchange(value = "auto.headers", type = ExchangeTypes.HEADERS, autoDelete = "true"),
//            arguments = {
//                    @Argument(name = "x-match", value = "all"),
//                    @Argument(name = "foo", value = "bar"),
//                    @Argument(name = "baz")
//            })
//    )
    public void listen2(Message message) {
    }

//    @RabbitListener(queues = "myqueue")
    public void listen21(Message message) {
        System.out.println(new String(message.getBody()));
    }
}
